package xj.graph2d.template.uml;

import xj.graph2d.DrawShape;
import xj.graph2d.DrawShapeConstants;
import xj.graph2d.GraphEdgeBase;
import xj.graph2d.Label;
import xj.graph2d.template.UMLSequenceDiagramTemplate;
import xj.graph2d.template.UMLTemplate;
import xj.model.uml.UMLMessage;

public class UMLMessageModelListener extends UMLAbstractModelListener {

  public UMLMessageModelListener() {
  }

  public UMLMessageModelListener(UMLTemplate template) {
    this(null, null, template);
  }

  public UMLMessageModelListener(Object model, DrawShape shape,
      UMLTemplate template) {
    super(model, shape, template);
  }

  @Override
  public void updateModel() {
    if (shape instanceof GraphEdgeBase && model instanceof UMLMessage) {
      GraphEdgeBase edge = (GraphEdgeBase) shape;
      UMLMessage mmodel = (UMLMessage) model;
      String msg = edge.getLabel(DrawShapeConstants.ROLE_EDGE_LABEL);
      mmodel.setMessage(msg);
    }
  }

  @Override
  public void updateShape() {
    if (shape instanceof GraphEdgeBase && model instanceof UMLMessage) {
      GraphEdgeBase edge = (GraphEdgeBase) shape;
      UMLMessage mmodel = (UMLMessage) model;
      String msg = mmodel.getMessage();
      if (msg != null) {
	Label lshape = setLabel(edge, DrawShapeConstants.ROLE_EDGE_LABEL, msg,
	    "message", true, UMLSequenceDiagramTemplate.MESSAGE_FONT,
	    UMLSequenceDiagramTemplate.MESSAGE_COLOR);
	if (lshape != null) {
	  lshape.setDisplayName("Message");
	}
      }
    }
  }

}
